package com.affectu.search.store;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.apache.lucene.store.FSDirectory;
import org.hibernate.search.SearchException;
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.store.DirectoryProvider;

/**
 * Oct 12, 2008
 * 
 * @author daniel nathan
 */
public class FSDirectoryProvider implements DirectoryProvider<FSDirectory> {

	private FSDirectory directory;
	private String indexName;

	public void initialize(String directoryProviderName, Properties properties,
			SearchFactoryImplementor searchFactoryImplementor) {
		File indexDir = DirectoryProviderHelper.getVerifiedIndexDir(
				directoryProviderName, properties, true);
		try {
			indexName = indexDir.getCanonicalPath();
			// this is cheap so it's not done in start()
			directory = DirectoryProviderHelper.createFSIndex(indexDir);
		} catch (IOException e) {
			throw new SearchException("Unable to initialize index: "
					+ directoryProviderName, e);
		}
	}

	public void start() {
		// all the process is done in initialize
	}

	public FSDirectory getDirectory() {
		return directory;
	}

	@Override
	public boolean equals(Object obj) {
		// this code is actually broken since the value change after initialize
		// call
		// but from a practical POV this is fine since we only call this method
		// after initialize call
		if (obj == this)
			return true;
		if (obj == null || !(obj instanceof FSDirectoryProvider))
			return false;
		return indexName.equals(((FSDirectoryProvider) obj).indexName);
	}

	@Override
	public int hashCode() {
		// this code is actually broken since the value change after initialize
		// call
		// but from a practical POV this is fine since we only call this method
		// after initialize call
		int hash = 11;
		return 37 * hash + indexName.hashCode();
	}
}
